package com.yixun.lbx.service.impl;

import com.yixun.lbx.entity.ProDetail;
import com.yixun.lbx.entity.YxProProductInfo;
import com.yixun.lbx.mapper.farm.ProductInfoMapper;
import com.yixun.lbx.service.ProductCheckService;
import com.yixun.lbx.service.ProductInfoServie;
import com.yixun.lbx.service.ProductYieldService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class ProductInfoServieImpl implements ProductInfoServie {

    private final ProductInfoMapper infoMapper;
    private final ProductCheckService checkService;
    private final ProductYieldService yieldService;

    public ProductInfoServieImpl(ProductInfoMapper infoMapper, ProductCheckService checkService, ProductYieldService yieldService) {
        this.infoMapper = infoMapper;
        this.checkService = checkService;
        this.yieldService = yieldService;
    }

    @Override
    public Integer getCount(HashMap<String, Object> map) {
        return infoMapper.getCount(map);
    }

    @Override
    public List<YxProProductInfo> getListByPage(HashMap<String, Object> map) {
        return infoMapper.getListByPage(map);
    }

    @Override
    public int addProductInfo(HashMap<String, Object> map) {
        map.put("addTime", getDate());
        return infoMapper.insertSelective(map);
    }

    @Override
    public int updateProductInfo(HashMap<String, Object> map) {
        map.put("updateTime", getDate());
        return infoMapper.updateByPrimaryKeySelective(map);
    }

    @Override
    public int deleteProductInfo(HashMap<String, Object> map) {
        checkService.deleteProductCheckByCpId(map);
        yieldService.deleteProductYieldByCpId(map);
        return infoMapper.deleteByPrimaryKey(map);
    }

    @Override
    public List<YxProProductInfo> getObjByArgs(YxProProductInfo productInfo) {
        return infoMapper.getObjByArgs(productInfo);
    }

    @Override
    public List<YxProProductInfo> getSearchListByPage(HashMap<String, String> map) {
        return infoMapper.getSearchListByPage(map);
    }
    
    @Override
    public YxProProductInfo getOneByCpId(String cpId) {
        return infoMapper.getOneByCpId(cpId);
    }
    
    @Override
    public ProDetail goProductDetail(String id) {
        return infoMapper.selectProductDetail(id);
    }
    
    private String getDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }
}
